package com.qf.cx.simplenms.link.model;
/*
 * 原始车辆数据转换为北向接口车辆信息数据utf8格式
 */

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.apache.log4j.Logger;
/*
 * 原始信息都是以byte信息存储的，而C中使用的事unsigned char java中没有改类型。对应使用有符号的byte存储
 * 如果byte中存储的是unsigned char的编码值时，需要先将byte转换成与unsigned char对应的int值。
 */

public class VehicleInfo {
	
	private static final Logger LOG = Logger.getLogger(VehicleInfo.class);	
	
	private static final String UTF8 = "UTF-8";
	public static final byte CAR_COLOR_SEL = 0;
	public static final byte CAR_USE_TYPE_SEL = 0;	
	public static final byte CAR_PLATE_TYPE_SEL = 0;		
	public static final byte CAR_TYPE_SEL = 0;

	
	private String cid;//
	private String fpdh;////发牌代号：1a2b3c4d56
	private String  syxz;//使用性质：私家车
	private String ccrq;//出厂日期
	private String cllx; //车辆类型  小型汽车
	private String gl;// 功率
	private String pl;//排量
	private String hpzl;//号牌种类
	private String hphmxh;//号牌号码序号
	private String jyyxq;//检验有效期
	private String qzbfq;//强制报废期
	private String csys;//车身颜色
	private String zkzl;//核定载客/总质量/总牵引质量参数
	private String cph;//车牌号
	
	public VehicleInfo() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getCid() {
		return cid;
	}

	public void setCid(byte[] cid) {
		
		int[] data = bytes2uchars(cid);
		String s = Arrays.toString(data);
		this.cid = s;
	}

	public String getFpdh() {
		return fpdh;
	}

	public void setFpdh(byte[] fpdh) {
		

		int procode = byte2uchar(fpdh[0]);
		int codecode = byte2uchar(fpdh[1]);
		
		String pro = ChineseChar.getProvince(procode);
		String code =  ChineseChar.getCarCode(codecode);
		this.fpdh = pro + code;

	}
	
	public String getSyxz() {
		return syxz;
	}

	public void setSyxz(byte syxz) {
		
		int data = byte2uchar(syxz);

		this.syxz = ChineseChar.getCarUseType(data, CAR_USE_TYPE_SEL);

	}

	public String getCcrq() {
		return ccrq;
	}

	public void setCcrq(byte[] ccrq) {
		
		int rq = byte2ToInt(ccrq);
		
		int year = 1990 + rq/12;
		int mon = 1 + rq%12;
		
		String date = String.format("%1$04d%2$02d", year,mon);
		
		//LOG.debug("ccrq date info " + date);
		
		this.ccrq = date;
	}

	public String getCllx() {
		return cllx;
	}

	public void setCllx(byte[] cllx) {
		
		int data = byte2ToInt(cllx);
		this.cllx = ChineseChar.getCarType(data, CAR_TYPE_SEL);

	}

	public String getGl() {
		return gl;
	}

	public void setGl(byte[] gl) {
		
		int data = byte2ToInt(gl);
		this.gl = String.valueOf(data);		
	}

	public String getPl() {
		return pl;
	}

	public void setPl(byte[] pl) {
		
		int data = byte2ToInt(pl);
		this.pl =  String.valueOf(data);
	}

	public String getHpzl() {
		return hpzl;
	}

	public void setHpzl(byte hpzl) {
		
		int data = byte2uchar(hpzl);

		this.hpzl =  ChineseChar.getCarPlateType(data, CAR_PLATE_TYPE_SEL);
	}

	public String getHphmxh() {
		return hphmxh;
	}

	public void setHphmxh(byte[] hphmxh) {
		
		String val = new String();
		for(byte code : hphmxh)
		{
			val += ChineseChar.getCarSerNum(code);
		}

		this.hphmxh =  val;
	
		
	}

	public String getJyyxq() {
		return jyyxq;
	}

	public void setJyyxq(byte[] jyyxq) {
		
		int data = byte2ToInt(jyyxq);
		int year = 2013 + data/12;
		int mon = 1 + data%12;
		String date = String.format("%1$04d%2$02d", year,mon);

		this.jyyxq =  date;
	
	}

	public String getQzbfq() {
		return qzbfq;
	}

	public void setQzbfq(byte qzbfq) {
		int data = byte2uchar(qzbfq);
	    this.qzbfq =  String.valueOf(data);	
	}

	public String getCsys() {
		return csys;
	}

	public void setCsys(byte csys) {
		int data = byte2uchar(csys);
		this.csys =  ChineseChar.getCarColor(data, CAR_COLOR_SEL);
	}

	public String getZkzl() {
		return zkzl;
	}

	public void setZkzl(byte[] zkzl) {
		
		int zl = byte2ToInt(zkzl);
		String val =  zl + "";
		this.zkzl =val;
	
	}

	public String getCph() {
		return cph;
	}

	public void setCph() {
		this.cph = getFpdh() + getHphmxh();
	}

    private static int byte2ToInt(byte[] bytes) { 
    	
    	//判断大小端？
        int high = byte2uchar(bytes[1]);
        int low = byte2uchar(bytes[0]);
        //LOG.debug("high " + high + "low " + low);
        return ((high << 8 & 0xFF00) | (low & 0xFF));
    }
    
	private static int[] bytes2uchars(byte[] s)
	{
		int[] d = new int[s.length];
		int tmp = 0;
		for(int i = 0; i < s.length; i++)
		{
			tmp  = s[i];
			tmp = tmp & 0xff;
			d[i] = tmp;
		}		
		return d;
	}
	private static int byte2uchar(byte s)
	{
		int tmp = 0;
	    tmp  = s;
		tmp = tmp & 0xff;
		return tmp;
	}	
}
